package com.andy.myFramework.util;

import javax.annotation.*;
import java.lang.annotation.*;

public class AnnotationUtils {
    /**
     * 判断类是否拥有某个注解（包含注解的注解）
     * @param cls
     * @param annotationClass
     * @param <A>
     * @return
     */
    public static <A extends Annotation> boolean hasAnnotation(Class<?> cls, Class<A> annotationClass){
        Annotation[] annotations = cls.getAnnotations();
        for (Annotation annotation : annotations) {
            //不考虑系统的注解
            if (annotation.annotationType() == Deprecated.class ||
                    annotation.annotationType() == SuppressWarnings.class ||
                    annotation.annotationType() == Override.class ||
                    annotation.annotationType() == PostConstruct.class ||
                    annotation.annotationType() == PreDestroy.class ||
                    annotation.annotationType() == Resource.class ||
                    annotation.annotationType() == Resources.class ||
                    annotation.annotationType() == Generated.class ||
                    annotation.annotationType() == Target.class ||
                    annotation.annotationType() == Retention.class ||
                    annotation.annotationType() == Documented.class ||
                    annotation.annotationType() == Inherited.class ) {
                continue;
            }

            if (annotation.annotationType() == annotationClass){
                return true;
            }else{
                return hasAnnotation(annotation.annotationType(),annotationClass);
            }
        }
        return false;
    }

    /**
     * 获取某个类的某一个注解(包含注解的注解)
     * @param cls
     * @param annotationClass
     * @param <A>
     * @return
     */
    public static <A extends Annotation> A getAnnotation(Class<?> cls, Class<A> annotationClass){
        Annotation[] annotations = cls.getAnnotations();
        for (Annotation annotation : annotations) {
            //不考虑系统的注解
            if (annotation.annotationType() == Deprecated.class ||
                    annotation.annotationType() == SuppressWarnings.class ||
                    annotation.annotationType() == Override.class ||
                    annotation.annotationType() == PostConstruct.class ||
                    annotation.annotationType() == PreDestroy.class ||
                    annotation.annotationType() == Resource.class ||
                    annotation.annotationType() == Resources.class ||
                    annotation.annotationType() == Generated.class ||
                    annotation.annotationType() == Target.class ||
                    annotation.annotationType() == Retention.class ||
                    annotation.annotationType() == Documented.class ||
                    annotation.annotationType() == Inherited.class ) {
                continue;
            }

            if (annotation.annotationType() == annotationClass){
                return (A)annotation;
            }else{
                A result = getAnnotation(annotation.annotationType(), annotationClass);
                if(result!=null) return result;
            }
        }
        return null;
    }
}
